//
//  UIView+FrameExt.m
//  YLCommon
//
//  Created by Apple on 2017/9/26.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "UIView+FrameExt.h"

@implementation UIView (FrameExt)
-(void)setYl_x:(CGFloat)yl_x
{
    CGRect rect =self.frame;
    rect.origin.x =yl_x;
    self.frame =rect;
}

-(void)setYl_y:(CGFloat)yl_y
{
    CGRect rect =self.frame;
    rect.origin.y =yl_y;
    self.frame =rect;
}

- (void)setYl_width:(CGFloat)yl_width
{
    CGRect rect =self.frame;
    rect.size.width =yl_width;
    self.frame =rect;
}

- (void)setYl_heigth:(CGFloat)yl_heigth
{
    CGRect rect =self.frame;
    rect.size.height =yl_heigth;
    self.frame =rect;
}

- (CGFloat)yl_width
{
    return self.frame.size.width;
}

- (CGFloat)yl_heigth
{
    return self.frame.size.height;
}

- (CGFloat)yl_x
{
    return self.frame.origin.x;
}
- (CGFloat)yl_y
{
    return self.frame.origin.y;
}

- (void)setYl_centerX:(CGFloat)yl_centerX
{
    CGPoint center =self.center;
    center.x =yl_centerX;
    self.center =center;
}

- (void)setYl_centerY:(CGFloat)yl_centerY
{
    CGPoint center =self.center;
    center.y =yl_centerY;
    self.center =center;
}

-(CGFloat)yl_centerX
{
    return self.center.x;
}

- (CGFloat)yl_centerY
{
    return self.center.y;
}

- (void)setYl_left:(CGFloat)yl_left
{
    CGRect frame = self.frame;
    frame.origin.x = yl_left;
    self.frame = frame;
}

- (CGFloat)yl_left{
   return self.frame.origin.x;
}




- (CGFloat)yl_top {
    return self.frame.origin.y;
}
- (void)setYl_top:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}


- (CGFloat)yl_right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setYl_right:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)yl_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setYl_bottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}


#pragma mark - API
// 判断View是否显示在屏幕上
- (BOOL)isDisplayedInScreen
{
    if (self == nil) {
        return FALSE;
    }
    
    CGRect screenRect = [UIScreen mainScreen].bounds;
    
    // 转换view对应window的Rect
    CGRect rect = [self convertRect:self.frame fromView:nil];
    if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {
        return FALSE;
    }
    
    // 若view 隐藏
    if (self.hidden) {
        return FALSE;
    }
    
    // 若没有superview
    if (self.superview == nil) {
        return FALSE;
    }
    
    // 若size为CGrectZero
    if (CGSizeEqualToSize(rect.size, CGSizeZero)) {
        return  FALSE;
    }
    
    // 获取 该view与window 交叉的 Rect
    CGRect intersectionRect = CGRectIntersection(rect, screenRect);
    if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {
        return FALSE;
    }
    
    return TRUE;
}

- (void)removeAllSubviews {
    while (self.subviews.count) {
        UIView* child = self.subviews.lastObject;
        [child removeFromSuperview];
    }
}
@end
